package cn.hty.mymsg.handler.action;

import cn.hty.mymsg.common.constant.CommonConstant;
import cn.hty.mymsg.common.domain.TaskInfo;
import cn.hty.mymsg.common.enums.DeduplicationType;
import cn.hty.mymsg.common.enums.EnumUtil;
import cn.hty.mymsg.common.pipeline.BusinessProcess;
import cn.hty.mymsg.common.pipeline.ProcessContext;
import cn.hty.mymsg.handler.deduplication.DeduplicationHolder;
import cn.hty.mymsg.handler.deduplication.DeduplicationParam;
import cn.hty.mymsg.service.ConfigService;
import cn.hutool.core.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class DeduplicationAction implements BusinessProcess<TaskInfo> {
    public static final String DEDUPLICATION_RULE_KEY = "deduplicationRule";
    @Autowired
    private ConfigService configService;
    @Autowired
    private DeduplicationHolder deduplicationHolder;
    @Override
    public void process(ProcessContext<TaskInfo> context) {
        TaskInfo taskInfo = context.getProcessModel();
        //1.获取配置信息
        String deduplicationConfig = configService.getProperty(DEDUPLICATION_RULE_KEY, CommonConstant.EMPTY_JSON_OBJECT);
        //2.根据配置信息去重
        //2.1获取去重总类
        List<Integer> deduplicationList = EnumUtil.getCodeList(DeduplicationType.class);
        for (Integer deduplicationType : deduplicationList) {
            DeduplicationParam deduplicationParam = deduplicationHolder.selectBuilder(deduplicationType).build(deduplicationConfig, taskInfo);
            //选择去重方式去重
            if (Objects.nonNull(deduplicationParam)){
                deduplicationHolder.selectService(deduplicationType).deduplication(deduplicationParam);
            }
        }
        if (CollUtil.isEmpty(taskInfo.getReceiver())) {
            context.setNeedBreak(true);
        }
    }
}
